<?php
declare (strict_types=1);

namespace app\admin\service\finance;

use app\admin\logic\finance\OrderFail as OrderFailLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DbException;
use think\Response;

/**
 * 分成后台管理
 */
class OrderFail extends BaseService
{
    /**
     * @Inject()
     *
     * @var OrderFailLogic
     */
    protected $logic;

    /**
     * list
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 24/07/25
     *
     * @return Response
     * @throws DbException
     */
    public function list()
    {
        $data = $this->logic->getFailList();

        // 定义字段
        $keyList = [
            'order'             => ListItem::make('custom')->title('订单号')->width(120),
            'user'              => ListItem::make('custom')->title('购买者'),
            'product_type'      => ListItem::make()->title('订单类型')->width(40),
            'product_title'     => ListItem::make()->title('产品名称'),
            'school_name'       => ListItem::make()->title('机构')->visible(request()->is_platform),
            'payment'           => ListItem::make()->title('订单金额'),
            'create_time'       => ListItem::make()->title('下单时间'),
            'payment_type_text' => ListItem::make()->title('支付方式'),
        ];

        $searchKey = [
            'order_no' => FormItem::make('input')->title(false)->placeholder('订单号'),
            'phone'    => FormItem::make('input')->title(false)->placeholder('购买者手机号')->maxlength(30),
            'time'     => FormItem::make('date')->title(false)->dateType(FormItem::option('DateType', 'DATE_RANGE'))->startPlaceholder('下单开始时间')->endPlaceholder('下单截止时间')->format('yyyy-MM-dd'),
        ];
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * handleFailOrder
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 24/07/25
     *
     * @param $orderNo
     * @return Response
     */
    public function handleFailOrder($orderNo)
    {
        $this->logic->handleFailOrder($orderNo);
        return $this->parseToData([], 1, '处理成功');
    }

}
